package cn.meng.base.controller;

import cn.meng.base.service.SalaryManageService;
import cn.meng.base.vo.BusinessException;
import cn.meng.base.vo.SalaryQueryRequest;
import cn.meng.base.vo.SalaryVO;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liululu
 * @description 薪酬管理
 * @date 2021-08-20 10:27
 **/
@RestController
@RequestMapping("/salaryManage")
public class SalaryManageController {

    @Autowired
    SalaryManageService service;

    @PostMapping("/insert")
    public String insertSalary(SalaryVO salaryVO) throws BusinessException {
        Map<String,Object> resultMessage = new HashMap<String,Object>();
        boolean result = service.insertSalary(salaryVO);
        if(result){
            resultMessage.put("success","添加成功");
        }else {
            resultMessage.put("error","添加失败");
        }
        return JSON.toJSONString(resultMessage);
    }

    @PostMapping("/update")
    public String updateSalary(SalaryVO salaryVO){
        Map<String,Object> resultMessage = new HashMap<String,Object>();
        boolean result = service.updateSalary(salaryVO);
        if(result){
            resultMessage.put("success","修改成功");
        }else {
            resultMessage.put("error","修改失败");
        }
        return JSON.toJSONString(resultMessage);
    }

    @PostMapping("/delete")
    public String deleteSalary(SalaryVO salaryVO){
        Map<String,Object> resultMessage = new HashMap<String,Object>();
        boolean result = service.deleteSalary(salaryVO);
        if(result){
            resultMessage.put("success","删除成功");
        }else {
            resultMessage.put("error","删除失败");
        }
        return JSON.toJSONString(resultMessage);
    }

    @GetMapping("/selectByPage")
    public PageInfo<SalaryVO> selectByPage(
            @RequestParam(name = "pageNum") Integer pageNum,@RequestParam(name = "pageSize")Integer pageSize,SalaryQueryRequest request){
        return service.selectByPage(pageNum,pageSize,request);
    }

    @GetMapping("/exportTemplate")
    public void exportTemplate(HttpServletResponse response, SalaryVO salaryVO) throws BusinessException {
        service.exportTemplate(response,salaryVO);
    }

}
